package dal

import (
	"context"
	"github.com/cloudwego/hertz/pkg/common/hlog"
	"gorm.io/gen"
	"gorm.io/gen/field"
	"mall_frontend_api/biz/config"
	"mall_frontend_api/biz/model"
)

type UserAddressDAO struct{}

type UserAddressListParams struct {
	Condition []gen.Condition
	Page      int
	Size      int
	Sort      []field.Expr
}

func newUserAddressDAO() *UserAddressDAO {
	return &UserAddressDAO{}
}

func (c *UserAddressDAO) MGet(ctx context.Context, ids []int64) (map[int64]*model.UserAddress, error) {
	list, err := UserAddress.WithContext(ctx).Where(
		UserAddress.BizID.Eq(config.Values.BizID),
		UserAddress.ID.In(ids...),
		UserAddress.DeletedAt.IsNull(),
	).Find()
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.MGetUserAddress err: %v", err)
		return nil, err
	}
	var data = make(map[int64]*model.UserAddress)
	for _, info := range list {
		data[info.ID] = info
	}
	return data, nil
}

func (c *UserAddressDAO) GetByConditions(ctx context.Context, condition []gen.Condition) (*model.UserAddress, error) {
	tx := UserAddress.WithContext(ctx).Where(
		UserAddress.BizID.Eq(config.Values.BizID),
		UserAddress.DeletedAt.IsNull(),
	)
	if len(condition) > 0 {
		tx = tx.Where(condition...)
	}
	info, err := tx.First()
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.GetUserAddressByConditions failed, %s", err.Error())
		return nil, err
	}
	return info, nil
}

func (c *UserAddressDAO) GetList(ctx context.Context, condition []gen.Condition, page, size int) ([]*model.UserAddress, error) {
	tx := UserAddress.WithContext(ctx).Where(UserAddress.BizID.Eq(config.Values.BizID), UserAddress.DeletedAt.IsNull())
	if len(condition) > 0 {
		tx = tx.Where(condition...)
	}
	if page > 0 && size > 0 {
		tx = tx.Offset((page - 1) * size).Limit(size)
	}
	list, err := tx.Find()
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.GetUserAddressList err: %v", err)
		return nil, err
	}
	return list, nil
}

func (c *UserAddressDAO) GetTotal(ctx context.Context, condition []gen.Condition) (int64, error) {
	total, err := UserAddress.WithContext(ctx).Where(UserAddress.BizID.Eq(config.Values.BizID), UserAddress.DeletedAt.IsNull()).Where(condition...).Count()
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.GetUserAddressTotal error: %v", err)
		return 0, err
	}
	return total, nil
}

func (c *UserAddressDAO) GetInfoByID(ctx context.Context, id int64) (*model.UserAddress, error) {
	info, err := UserAddress.WithContext(ctx).Where(UserAddress.BizID.Eq(config.Values.BizID), UserAddress.DeletedAt.IsNull()).Where(UserAddress.ID.Eq(id)).First()
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.GetUserAddressInfoByID error: %v", err)
		return nil, err
	}
	return info, nil
}

func (c *UserAddressDAO) GetListWithTotal(ctx context.Context, params *UserAddressListParams) ([]*model.UserAddress, int64, error) {
	tx := UserAddress.WithContext(ctx).Where(UserAddress.BizID.Eq(config.Values.BizID), UserAddress.DeletedAt.IsNull())
	if len(params.Condition) > 0 {
		tx = tx.Where(params.Condition...)
	}
	if len(params.Sort) > 0 {
		tx = tx.Order(params.Sort...)
	}
	list, total, err := tx.FindByPage((params.Page-1)*params.Size, params.Size)
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.UserAddress.GetListWithTotal err: %v", err)
		return nil, 0, err
	}
	return list, total, nil
}

func (c *UserAddressDAO) Create(ctx context.Context, data *model.UserAddress) (*model.UserAddress, error) {
	if err := UserAddress.WithContext(ctx).Create(data); err != nil {
		hlog.CtxErrorf(ctx, "dal.CreateUserAddress error: %v", err)
		return nil, err
	}
	return data, nil
}

func (c *UserAddressDAO) CreateWithTx(ctx context.Context, tx *Query, data *model.UserAddress) (*model.UserAddress, error) {
	if err := tx.UserAddress.WithContext(ctx).Create(data); err != nil {
		hlog.CtxErrorf(ctx, "dal.CreateUserAddress error: %v", err)
		return nil, err
	}
	return data, nil
}

func (c *UserAddressDAO) BatchCreate(ctx context.Context, data []*model.UserAddress) error {
	if err := UserAddress.WithContext(ctx).CreateInBatches(data, 20); err != nil {
		hlog.CtxErrorf(ctx, "dal.CreateInBatchesUserAddress error: %v", err)
		return err
	}
	return nil
}

func (c *UserAddressDAO) BatchCreateWithTx(ctx context.Context, tx *Query, data []*model.UserAddress) ([]*model.UserAddress, error) {
	if err := tx.UserAddress.WithContext(ctx).CreateInBatches(data, 20); err != nil {
		hlog.CtxErrorf(ctx, "dal.BatchCreateWithTxUserAddress error: %v", err)
		return nil, err
	}
	return data, nil
}

func (c *UserAddressDAO) Update(ctx context.Context, condition []gen.Condition, fields []field.AssignExpr) error {
	_, err := UserAddress.WithContext(ctx).Where(UserAddress.BizID.Eq(config.Values.BizID)).Where(condition...).UpdateSimple(fields...)
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.UpdateUserAddress error: %v", err)
		return err
	}
	return nil
}

func (c *UserAddressDAO) UpdateWithTx(ctx context.Context, tx *Query, condition []gen.Condition, fields []field.AssignExpr) error {
	_, err := tx.UserAddress.WithContext(ctx).Where(UserAddress.BizID.Eq(config.Values.BizID)).Where(condition...).UpdateSimple(fields...)
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.UpdateUserAddress error: %v", err)
		return err
	}
	return nil
}

func (c *UserAddressDAO) Delete(ctx context.Context, condition []gen.Condition) error {
	_, err := UserAddress.WithContext(ctx).Where(UserAddress.BizID.Eq(config.Values.BizID)).Where(condition...).Delete()
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.DeleteUserAddress error: %v", err)
		return err
	}
	return nil
}

func (c *UserAddressDAO) DeleteWithTx(ctx context.Context, tx *Query, condition []gen.Condition) error {
	_, err := tx.UserAddress.WithContext(ctx).Where(UserAddress.BizID.Eq(config.Values.BizID)).Where(condition...).Delete()
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.DeleteUserAddressWithTx error: %v", err)
		return err
	}
	return nil
}
